/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Jun 23, 2012  9:29:22 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : commons
 * File Name : ServletCommon.java
 * TODO
 */
package com.tis.interfaces.commons;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

import promis.utility.CommonService;

/**
 * @author chatree
 *
 */
public class ServletCommon {
	
	private static final Logger logger = LoggerFactory.getLogger(ServletCommon.class);

	protected String company;
	protected String project;
	protected String buildingPhase;
	protected String zoneCode;
	protected String unitNumber;
	protected String sapPlantCode;
	protected String matritalCode;
	protected String customerCode;
	protected String status;
	protected String receipt_status;
	protected String bs_cs_status;
	protected String action;
	protected String syncIsstaging;
	protected String documentNo;
	protected String contractNo;
	protected String BookingOrContarctNo;
	protected String ReceipteddocumentNo;
	protected String CN_DocumentNo;
	protected String contractRefNo;
	protected String processAction;
	protected String processActionBy;
	
	/**
	 * refType such as CT, VO
	 */	
	protected String refType;
	
	
	public ServletCommon() {
		super();
	}
	
	private ServletParam servletParam;
	
	
	/**
	 * @return the servletParam
	 * Date =Nov 14, 2013 : 2:07:28 PM 
	 * CreateBy=Chatree Siangklom
	 * ServletCommon.java ServletParam
	 * TODO
	 */
	public ServletParam getServletParam() {
		return this.servletParam;
	}

	/**
	 * @param servletParam the servletParam to set
	 * Date =Nov 14, 2013 : 2:07:28 PM 
	 * CreateBy=Chatree Siangklom
	 *  ServletCommon.java ServletParam
	 * TODO
	 */
	public void setServletParam(ServletParam servletParam) {
		this.servletParam = servletParam;
	}
	
	protected HttpEntity<byte[]> ToXml(XmlResultback xmlResultback) {
		try{
			if(xmlResultback != null){
				String xml = xmlResultback.GennerateToXml(); 
				logger.info("xml=" + xml);
				
			    byte[] documentBody = xml.getBytes();
			    HttpHeaders header = new HttpHeaders();
			    header.setContentType(new MediaType("application", "xml"));
			    header.setContentLength(documentBody.length);
			    
			    return new HttpEntity<byte[]>(documentBody, header);
			}
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return null;
	}

	protected Map initParam(HttpServletRequest request) throws ServletException, IOException {
		Map mapValue = new HashMap<String, Object>();
		
		ServletParam servletParam = new ServletParam();
		
		this.action = CommonService.IsNullRString(request.getParameter("action"));
		servletParam.setAction(action);
		mapValue.put("action", action);
		
		this.syncIsstaging = CommonService.IsNullRString(request.getParameter("syncIsstaging"));
		servletParam.setSyncIsstaging(syncIsstaging);
		mapValue.put("syncIsstaging", syncIsstaging);
		
		this.company = CommonService.IsNullRString(request.getParameter("company"));
		servletParam.setCompany(company);
		mapValue.put("company", company);
				
		this.project = CommonService.IsNullRString(request.getParameter("project"));
		servletParam.setProject(project);
		mapValue.put("project", project);
				
		this.buildingPhase = CommonService.IsNullRString(request.getParameter("buildingPhase"));
		servletParam.setBuildingPhase(buildingPhase);
		mapValue.put("buildingPhase", buildingPhase);
		
		this.zoneCode = CommonService.IsNullRString(request.getParameter("zoneCode"));
		servletParam.setZoneCode(zoneCode);
		mapValue.put("zoneCode", zoneCode);
		
		this.unitNumber = CommonService.IsNullRString(request.getParameter("unitNumber"));
		servletParam.setUnitNumber(unitNumber);
		mapValue.put("unitNumber", unitNumber);
		
		//Construction
		this.documentNo = CommonService.IsNullRString(request.getParameter("documentNo"));
		servletParam.setDocumentNo(documentNo);
		mapValue.put("documentNo", documentNo);
		
		this.refType = CommonService.IsNullRString(request.getParameter("refType"));
		servletParam.setRefType(refType);
		mapValue.put("refType", refType);
		
		this.matritalCode = CommonService.IsNullRString(request.getParameter("matritalCode"));
		servletParam.setMatritalCode(matritalCode);
		mapValue.put("matritalCode", matritalCode);
				
		this.sapPlantCode = CommonService.IsNullRString(request.getParameter("sapPlantCode"));
		servletParam.setSapPlantCode(sapPlantCode);
		mapValue.put("sapPlantCode", sapPlantCode);
				
		this.customerCode = CommonService.IsNullRString(request.getParameter("customerCode"));
		servletParam.setCustomerCode(customerCode);
		mapValue.put("customerCode", customerCode);
		
		this.status = CommonService.IsNullRString(request.getParameter("status"));
		servletParam.setStatus(status);
		mapValue.put("status", status);
		
		this.contractNo = CommonService.IsNullRString(request.getParameter("contractNo"));
		servletParam.setContractNo(contractNo);
		mapValue.put("contractNo", contractNo);
		
		this.BookingOrContarctNo = CommonService.IsNullRString(request.getParameter("BookingOrContarctNo"));
		servletParam.setBookingOrContarctNo(BookingOrContarctNo);
		mapValue.put("BookingOrContarctNo", BookingOrContarctNo);
		
		this.ReceipteddocumentNo = CommonService.IsNullRString(request.getParameter("ReceipteddocumentNo"));
		servletParam.setReceipteddocumentNo(ReceipteddocumentNo);
		mapValue.put("ReceipteddocumentNo", ReceipteddocumentNo);
		
		this.CN_DocumentNo = CommonService.IsNullRString(request.getParameter("CN_DocumentNo"));
		servletParam.setCN_DocumentNo(CN_DocumentNo);
		mapValue.put("CN_DocumentNo", CN_DocumentNo);
		
		this.receipt_status = CommonService.IsNullRString(request.getParameter("receipt_status"));
		servletParam.setReceipt_status(receipt_status);
		mapValue.put("receipt_status", receipt_status);
		
		this.bs_cs_status = CommonService.IsNullRString(request.getParameter("bs_cs_status"));
		servletParam.setBs_cs_status(bs_cs_status);
		mapValue.put("bs_cs_status", bs_cs_status);
		
		this.contractRefNo = request.getParameter("contractRefNo");
		servletParam.setContractNo(contractNo);
		mapValue.put("contractRefNo", contractRefNo);
		
		this.processAction = request.getParameter("processAction");
		servletParam.setProcessAction(processAction);
		mapValue.put("processAction", processAction);
		
		this.processActionBy = request.getParameter("processActionBy");
		servletParam.setProcessActionBy(processActionBy);
		mapValue.put("processActionBy", processActionBy);
		
		
		setServletParam(servletParam);
		logger.info(getServletParam().toString());
		
		return mapValue;
	}
	
	class ServletParam {
		private String company;
		private String project;
		private String buildingPhase;
		private String zoneCode;
		private String unitNumber;
		private String sapPlantCode;
		private String matritalCode;
		private String customerCode;
		private String status;
		private String receipt_status;
		private String bs_cs_status;
		private String action;
		private String syncIsstaging;
		private String documentNo;
		private String contractNo;
		private String BookingOrContarctNo;
		private String ReceipteddocumentNo;
		private String CN_DocumentNo;
		private String contractRefNo;
		private String processAction;
		private String processActionBy;
		/**
		 * refType such as CT, VO
		 */	
		private String refType;
		
		@Override public String toString() { return org.apache.commons.lang.builder.ToStringBuilder.reflectionToString(this);}
		
		
		
		/**
		 * @return the refType
		 * Date =Nov 14, 2013 : 2:09:59 PM 
		 * CreateBy=Chatree Siangklom
		 * ServletCommon.java String
		 * TODO
		 */
		public String getRefType() {
			return this.refType;
		}



		/**
		 * @param refType the refType to set
		 * Date =Nov 14, 2013 : 2:09:59 PM 
		 * CreateBy=Chatree Siangklom
		 *  ServletCommon.java String
		 * TODO
		 */
		public void setRefType(String refType) {
			this.refType = refType;
		}



		/**
		 * @return the company
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 * ServletCommon.java String
		 * TODO
		 */
		public String getCompany() {
			return this.company;
		}
		/**
		 * @param company the company to set
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 *  ServletCommon.java String
		 * TODO
		 */
		public void setCompany(String company) {
			this.company = company;
		}
		/**
		 * @return the project
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 * ServletCommon.java String
		 * TODO
		 */
		public String getProject() {
			return this.project;
		}
		/**
		 * @param project the project to set
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 *  ServletCommon.java String
		 * TODO
		 */
		public void setProject(String project) {
			this.project = project;
		}
		/**
		 * @return the buildingPhase
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 * ServletCommon.java String
		 * TODO
		 */
		public String getBuildingPhase() {
			return this.buildingPhase;
		}
		/**
		 * @param buildingPhase the buildingPhase to set
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 *  ServletCommon.java String
		 * TODO
		 */
		public void setBuildingPhase(String buildingPhase) {
			this.buildingPhase = buildingPhase;
		}
		/**
		 * @return the zoneCode
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 * ServletCommon.java String
		 * TODO
		 */
		public String getZoneCode() {
			return this.zoneCode;
		}
		/**
		 * @param zoneCode the zoneCode to set
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 *  ServletCommon.java String
		 * TODO
		 */
		public void setZoneCode(String zoneCode) {
			this.zoneCode = zoneCode;
		}
		/**
		 * @return the unitNumber
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 * ServletCommon.java String
		 * TODO
		 */
		public String getUnitNumber() {
			return this.unitNumber;
		}
		/**
		 * @param unitNumber the unitNumber to set
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 *  ServletCommon.java String
		 * TODO
		 */
		public void setUnitNumber(String unitNumber) {
			this.unitNumber = unitNumber;
		}
		/**
		 * @return the sapPlantCode
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 * ServletCommon.java String
		 * TODO
		 */
		public String getSapPlantCode() {
			return this.sapPlantCode;
		}
		/**
		 * @param sapPlantCode the sapPlantCode to set
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 *  ServletCommon.java String
		 * TODO
		 */
		public void setSapPlantCode(String sapPlantCode) {
			this.sapPlantCode = sapPlantCode;
		}
		/**
		 * @return the matritalCode
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 * ServletCommon.java String
		 * TODO
		 */
		public String getMatritalCode() {
			return this.matritalCode;
		}
		/**
		 * @param matritalCode the matritalCode to set
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 *  ServletCommon.java String
		 * TODO
		 */
		public void setMatritalCode(String matritalCode) {
			this.matritalCode = matritalCode;
		}
		/**
		 * @return the customerCode
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 * ServletCommon.java String
		 * TODO
		 */
		public String getCustomerCode() {
			return this.customerCode;
		}
		/**
		 * @param customerCode the customerCode to set
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 *  ServletCommon.java String
		 * TODO
		 */
		public void setCustomerCode(String customerCode) {
			this.customerCode = customerCode;
		}
		/**
		 * @return the status
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 * ServletCommon.java String
		 * TODO
		 */
		public String getStatus() {
			return this.status;
		}
		/**
		 * @param status the status to set
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 *  ServletCommon.java String
		 * TODO
		 */
		public void setStatus(String status) {
			this.status = status;
		}
		/**
		 * @return the receipt_status
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 * ServletCommon.java String
		 * TODO
		 */
		public String getReceipt_status() {
			return this.receipt_status;
		}
		/**
		 * @param receipt_status the receipt_status to set
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 *  ServletCommon.java String
		 * TODO
		 */
		public void setReceipt_status(String receipt_status) {
			this.receipt_status = receipt_status;
		}
		/**
		 * @return the bs_cs_status
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 * ServletCommon.java String
		 * TODO
		 */
		public String getBs_cs_status() {
			return this.bs_cs_status;
		}
		/**
		 * @param bs_cs_status the bs_cs_status to set
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 *  ServletCommon.java String
		 * TODO
		 */
		public void setBs_cs_status(String bs_cs_status) {
			this.bs_cs_status = bs_cs_status;
		}
		/**
		 * @return the action
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 * ServletCommon.java String
		 * TODO
		 */
		public String getAction() {
			return this.action;
		}
		/**
		 * @param action the action to set
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 *  ServletCommon.java String
		 * TODO
		 */
		public void setAction(String action) {
			this.action = action;
		}
		/**
		 * @return the syncIsstaging
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 * ServletCommon.java String
		 * TODO
		 */
		public String getSyncIsstaging() {
			return this.syncIsstaging;
		}
		/**
		 * @param syncIsstaging the syncIsstaging to set
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 *  ServletCommon.java String
		 * TODO
		 */
		public void setSyncIsstaging(String syncIsstaging) {
			this.syncIsstaging = syncIsstaging;
		}
		/**
		 * @return the documentNo
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 * ServletCommon.java String
		 * TODO
		 */
		public String getDocumentNo() {
			return this.documentNo;
		}
		/**
		 * @param documentNo the documentNo to set
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 *  ServletCommon.java String
		 * TODO
		 */
		public void setDocumentNo(String documentNo) {
			this.documentNo = documentNo;
		}
		/**
		 * @return the contractNo
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 * ServletCommon.java String
		 * TODO
		 */
		public String getContractNo() {
			return this.contractNo;
		}
		/**
		 * @param contractNo the contractNo to set
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 *  ServletCommon.java String
		 * TODO
		 */
		public void setContractNo(String contractNo) {
			this.contractNo = contractNo;
		}
		/**
		 * @return the bookingOrContarctNo
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 * ServletCommon.java String
		 * TODO
		 */
		public String getBookingOrContarctNo() {
			return this.BookingOrContarctNo;
		}
		/**
		 * @param bookingOrContarctNo the bookingOrContarctNo to set
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 *  ServletCommon.java String
		 * TODO
		 */
		public void setBookingOrContarctNo(String bookingOrContarctNo) {
			this.BookingOrContarctNo = bookingOrContarctNo;
		}
		/**
		 * @return the receipteddocumentNo
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 * ServletCommon.java String
		 * TODO
		 */
		public String getReceipteddocumentNo() {
			return this.ReceipteddocumentNo;
		}
		/**
		 * @param receipteddocumentNo the receipteddocumentNo to set
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 *  ServletCommon.java String
		 * TODO
		 */
		public void setReceipteddocumentNo(String receipteddocumentNo) {
			this.ReceipteddocumentNo = receipteddocumentNo;
		}
		/**
		 * @return the cN_DocumentNo
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 * ServletCommon.java String
		 * TODO
		 */
		public String getCN_DocumentNo() {
			return this.CN_DocumentNo;
		}
		/**
		 * @param cN_DocumentNo the cN_DocumentNo to set
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 *  ServletCommon.java String
		 * TODO
		 */
		public void setCN_DocumentNo(String cN_DocumentNo) {
			this.CN_DocumentNo = cN_DocumentNo;
		}
		/**
		 * @return the contractRefNo
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 * ServletCommon.java String
		 * TODO
		 */
		public String getContractRefNo() {
			return this.contractRefNo;
		}
		/**
		 * @param contractRefNo the contractRefNo to set
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 *  ServletCommon.java String
		 * TODO
		 */
		public void setContractRefNo(String contractRefNo) {
			this.contractRefNo = contractRefNo;
		}
		/**
		 * @return the processAction
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 * ServletCommon.java String
		 * TODO
		 */
		public String getProcessAction() {
			return this.processAction;
		}
		/**
		 * @param processAction the processAction to set
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 *  ServletCommon.java String
		 * TODO
		 */
		public void setProcessAction(String processAction) {
			this.processAction = processAction;
		}
		/**
		 * @return the processActionBy
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 * ServletCommon.java String
		 * TODO
		 */
		public String getProcessActionBy() {
			return this.processActionBy;
		}
		/**
		 * @param processActionBy the processActionBy to set
		 * Date =Nov 14, 2013 : 2:02:10 PM 
		 * CreateBy=Chatree Siangklom
		 *  ServletCommon.java String
		 * TODO
		 */
		public void setProcessActionBy(String processActionBy) {
			this.processActionBy = processActionBy;
		}
	}
}
